﻿#include "SqlManage.h"
#include <QGroupBox>
#include <QFormLayout>
#include <QApplication>
#include <QSettings>
#include <QMetaType>
#include "WgtSqlConfig.h"

#pragma execution_character_set("utf-8")

SqlManage::SqlManage(QObject *parent)
    : QObject{parent}
{
#if QT_VERSION >= 0x060000
// Qt 6 的代码
#else
    // Qt 5 的代码
    qRegisterMetaTypeStreamOperators<SqlLinkParam>("SqlLinkParam");
#endif

    configWriteOne = [](QString name, const SqlLinkParam& config){
        QSettings iniFile(QApplication::applicationDirPath() + "/Configs/Config.ini", QSettings::IniFormat);
        iniFile.setValue("SqlLinkParam/" + name, QVariant::fromValue(config));
    };
    configReadOne = [](QString name)->SqlLinkParam{
        QSettings iniFile(QApplication::applicationDirPath() + "/Configs/Config.ini", QSettings::IniFormat);
        SqlLinkParam config = iniFile.value("SqlLinkParam/" + name).value<SqlLinkParam>();
        return config;
    };

    wgtSqlManage = new WgtSqlManage(tr("数据库配置"));
    wgtSqlStatus = new WgtSqlStatus(tr("数据库状态"));
    wgtSqlStatus->setMinimumWidth(200);
}

void SqlManage::setupRecord()
{
    SqlLinkParam param = configReadOne(TableNameRecord);
    IDataBaseWorker* record = new IDataBaseWorker(param);
    QThread* thr = new QThread();
    record->moveToThread(thr);
    connect(thr, &QThread::started, record, &IDataBaseWorker::onInit);
    connect(qApp, &QApplication::aboutToQuit, record, &IDataBaseWorker::onQuit);
    connect(record, &QObject::destroyed, thr, &QThread::quit);
    connect(thr, &QThread::finished, thr, &QThread::deleteLater);
    mapSqlWorker.insert(TableNameRecord, record);
    mMapSqlThread.insert(TableNameRecord, thr);
    thr->start();

    WgtSqlConfig* wgtRecord = new WgtSqlConfig();
    wgtSqlManage->addConfigWgt(tr("记录数据库"), wgtRecord);
    connect(wgtRecord, &WgtSqlConfig::sigConfigChanged, this, [&](SqlLinkParam param){
        configWriteOne(TableNameSolution, param);
        mapSqlWorker.value(TableNameRecord)->setLinkParam(param);
    });
}

void SqlManage::setupStatistics()
{
    SqlLinkParam param = configReadOne(TableNameStatistics);
    IDataBaseWorker* statistics = new IDataBaseWorker(param);
    QThread* thr = new QThread();
    statistics->moveToThread(thr);
    connect(thr, &QThread::started, statistics, &IDataBaseWorker::onInit);
    connect(qApp, &QApplication::aboutToQuit, statistics, &IDataBaseWorker::onQuit);
    connect(statistics, &QObject::destroyed, thr, &QThread::quit);
    connect(thr, &QThread::finished, thr, &QThread::deleteLater);
    mapSqlWorker.insert(TableNameStatistics, statistics);
    mMapSqlThread.insert(TableNameStatistics, thr);
    thr->start();

    WgtSqlConfig* wgtStatistics = new WgtSqlConfig();
    wgtSqlManage->addConfigWgt(tr("统计数据库"), wgtStatistics);
    connect(wgtStatistics, &WgtSqlConfig::sigConfigChanged, this, [&](SqlLinkParam param){
        configWriteOne(TableNameSolution, param);
        mapSqlWorker.value(TableNameStatistics)->setLinkParam(param);
    });
}

void SqlManage::setupUser()
{
    SqlLinkParam linkPar = configReadOne(TableNameUser);
    IDataBaseWorker* user = new IDataBaseWorker(linkPar);
    mapSqlWorker.insert(TableNameUser, user);

    WgtSqlConfig* wgtUser = new WgtSqlConfig();
    wgtSqlManage->addConfigWgt(tr("用户数据库"), wgtUser);
    connect(wgtUser, &WgtSqlConfig::sigConfigChanged, this, [&](SqlLinkParam param){
        configWriteOne(TableNameSolution, param);
        mapSqlWorker.value(TableNameUser)->setLinkParam(param);
    });
}

void SqlManage::setupSolution()
{
    SqlLinkParam linkPar = configReadOne(TableNameSolution);
    IDataBaseWorker* sol = new IDataBaseWorker(linkPar);
    mapSqlWorker.insert(TableNameSolution, sol);

    WgtSqlConfig* wgtSol = new WgtSqlConfig();
    wgtSqlManage->addConfigWgt(tr("方案数据库"), wgtSol);
    connect(wgtSol, &WgtSqlConfig::sigConfigChanged, this, [&](SqlLinkParam param){
        configWriteOne(TableNameSolution, param);
        mapSqlWorker.value(TableNameSolution)->setLinkParam(param);
    });
}

void SqlManage::setConfigWriteOneFunc(void (*func)(QString name, const SqlLinkParam &))
{
    configWriteOne = func;
}

void SqlManage::setConfigReadOneFunc(SqlLinkParam (*func)(QString))
{
    configReadOne = func;
}


